function loop_size(node) {
  var temp = true
  var new_node = node
  var result = []
  while (temp || !new_node) {
    result.push(new_node)
    new_node = new_node.next
    if (new_node in result) {
      temp = false
      var size = result.length - result.indexOf(new_node.next) + 1
    }
  }
  return size == 2 ? (size - 1) : size
}



function loop_size(node) {
  var nodes = [], n = node;

  while (nodes.indexOf(n) === -1) {
    nodes.push(n);
    n = n.getNext();
  }

  return nodes.length - nodes.indexOf(n);
}

function loop_size(node) {
  var turtle = node;
  var rabbit = node;

  // Find a point in the loop.  Any point will do!
  // Since the rabbit moves faster than the turtle
  // and the kata guarantees a loop, the rabbit will
  // eventually catch up with the turtle.
  do {
    turtle = turtle.getNext();
    rabbit = rabbit.getNext().getNext();
  }
  while (turtle != rabbit);

  // The turtle and rabbit are now on the same node,
  // but we know that node is in a loop.  So now we
  // keep the turtle motionless and move the rabbit
  // until it finds the turtle again, counting the
  // nodes the rabbit visits in the mean time.
  var count = 0;
  do {
    ++count;
    rabbit = rabbit.getNext();
  }
  while (turtle != rabbit);

  // voila
  return count;
}


function loop_size(node) {
  var map = new WeakMap(), i = 0;

  while (map.get(node) === void 0) {
    map.set(node, ++i);
    node = node.getNext();
  }

  return i - map.get(node) + 1;
}

function loop_size(node) {
  let lastIndex = 0;
  let indices = new WeakMap();
  while (indices.get(node) == null) {
    indices.set(node, lastIndex++);
    node = node.next;
  }
  return lastIndex - indices.get(node);
}

function loop_size(node) {
  var len = 2,
    current = node;
  while (true) {
    for (var i = 1; i <= len; i++) {
      node = node.getNext();
      if (node == current) {
        return i;
      }
    }
    current = node;
    len *= 2;
  }
}

const loop_size = node => {
  let visited = [];
  while (!visited.includes(node)) {
    visited.push(node);
    node = node.next;
  }
  let distance = visited.length - visited.indexOf(node)
  return distance;
}